package com.bgs.eat.rider.service.impl;

import com.bgs.eat.commodityOrder.entity.CommodityOrderEntity;
import com.bgs.eat.commodityOrder.mapper.CommodityOrderMapper;
import com.bgs.eat.config.CommonResult;
import com.bgs.eat.rider.entity.Riderinformation;
import com.bgs.eat.rider.entity.UserProtal;
import com.bgs.eat.rider.entity.VO.AccountOrders;
import com.bgs.eat.rider.entity.dto.registeredRiderDto;
import com.bgs.eat.rider.mapper.riderMapper;
import com.bgs.eat.rider.mapper.riderinformationMapper;
import com.bgs.eat.rider.service.riderService;
import com.bgs.eat.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class riderServiceImpl implements riderService {
    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private riderMapper riderMapper;

    @Autowired
    private riderinformationMapper riderinformationMapper;

    @Autowired
    private CommodityOrderMapper commodityOrderMapper;
    /* *
     * @Author: beisheng
     * @Descriprion: 申请成为骑手
     * @Date: 2022/10/13 20:06
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: [phone, code]
     */

    @Transactional
    @Override
    public CommonResult registeredRider(registeredRiderDto registeredRiderDto) {
        Object o = redisUtil.get(registeredRiderDto.getPhone() + registeredRiderDto.getCodeMark());
        if (registeredRiderDto.getPhoneCode().equalsIgnoreCase((String) o)) {
            Example example = new Example(UserProtal.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("phone",registeredRiderDto.getPhone());
            UserProtal userProtal = new UserProtal();
            UserProtal userProtals = riderMapper.selectOneByExample(example);
            BeanUtils.copyProperties(userProtals,userProtal);
            userProtal.setAuditId(1);
            riderMapper.updateByExample(userProtal,example);
            Riderinformation riderinformation = new Riderinformation();
            riderinformation.setUserProtalId(userProtal.getId());
            riderinformation.setWorkingCondition(0);
            riderinformation.setProvinceId(registeredRiderDto.getProviceId());
            riderinformation.setProvince(registeredRiderDto.getProvice());
            riderinformation.setCountyId(registeredRiderDto.getAreaId());
            riderinformation.setCounty(registeredRiderDto.getAreaName());
            riderinformation.setCity(registeredRiderDto.getCityName());
            riderinformation.setCityId(registeredRiderDto.getCityId());
            riderinformation.setDelFlag(0);
            riderinformation.setCreateTime(new Date());
            riderinformation.setUpdateTime(new Date());
            riderinformationMapper.insert(riderinformation);
            return CommonResult.ok();
        }else {
            return CommonResult.error("999");
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 验证验证码
     * @Date: 2022/10/14 8:56
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: [phone, code]
     */

    @Override
    public CommonResult verificationPhoneCode(String phone, String code,String codeMark) {
        Object o = redisUtil.get(phone + codeMark);
        if (code.equalsIgnoreCase((String) o)) {
            return CommonResult.ok();
        }else {
            return CommonResult.error("999");
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 用户修改密码
     * @Date: 2022/10/14 10:15
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: [phone]
     */

    @Override
    public CommonResult updatePassword(String phone, String newPassword) {
        Example example = new Example(UserProtal.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",phone);
        UserProtal userProtal = new UserProtal();
        UserProtal userProtals = riderMapper.selectOneByExample(example);
        BeanUtils.copyProperties(userProtals,userProtal);
        userProtal.setPassword(DigestUtils.md5DigestAsHex(newPassword.getBytes()));
        riderMapper.updateByExample(userProtal,example);
        return CommonResult.ok();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改骑手状态【上线-1,下线-0】
     * @Date: 2022/10/21 13:58
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: [phone, name]
     */

    @Override
    public CommonResult updateStatus(String phone, String name) {
        Example example = new Example(Riderinformation.class);
        Example.Criteria criteria = example.createCriteria();

        Example exampleUser = new Example(UserProtal.class);
        Example.Criteria criteriaUser = exampleUser.createCriteria();

        criteriaUser.andEqualTo("phone",phone);
        UserProtal userProtal = riderMapper.selectOneByExample(exampleUser);

        criteria.andEqualTo("userProtalId",userProtal.getId());
        Riderinformation riderinformation = riderinformationMapper.selectOneByExample(example);

        Riderinformation riderinformation1 = new Riderinformation();
        BeanUtils.copyProperties(riderinformation,riderinformation1);
        if(name.equals("下线中")){
            riderinformation1.setWorkingCondition(1);
        }else {{
            riderinformation1.setWorkingCondition(0);
        }}
        riderinformationMapper.updateByExample(riderinformation1,example);

        return CommonResult.ok();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 订单配送完成后骑手账户余额变化，骑手订单统计+1
     * @Date: 2022/10/26 13:50
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: [id,riderId, distributionPrice]
     */

    @Override
    public CommonResult todayTask(Integer id,Integer riderId, Double distributionPrice) {
        Riderinformation riderinformation = new Riderinformation();
        Example example = new Example(Riderinformation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",riderId);
        Riderinformation riderinformation1 = riderinformationMapper.selectOneByExample(example);


        BeanUtils.copyProperties(riderinformation1,riderinformation);
        riderinformation.setBalance(riderinformation.getBalance()+distributionPrice);
        riderinformation.setOrderQuantity(riderinformation.getOrderQuantity()+1);
        riderinformation.setTodayMoney(riderinformation.getTodayMoney()+distributionPrice);
        riderinformation.setOrdersNumber(riderinformation.getOrdersNumber()+1);
        riderinformationMapper.updateByExample(riderinformation,example);

        Example exampleOrder = new Example(CommodityOrderEntity.class);
        Example.Criteria criteriaOrder = exampleOrder.createCriteria();
        criteriaOrder.andEqualTo("id",id);
        CommodityOrderEntity commodityOrderEntity = commodityOrderMapper.selectOneByExample(exampleOrder);
        CommodityOrderEntity commodityOrderEntity1 = new CommodityOrderEntity();
        BeanUtils.copyProperties(commodityOrderEntity,commodityOrderEntity1);
        commodityOrderEntity1.setDistributionStatus(5);
        commodityOrderMapper.updateByExample(commodityOrderEntity1,exampleOrder);

        return CommonResult.ok();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 查询骑手今日预计收入，今日完成订单量
     * @Date: 2022/10/26 14:50
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: [riderId]
     */

    @Override
    public CommonResult getAccountOrders(Integer id) {
        AccountOrders accountOrders = new AccountOrders();
        Example example = new Example(Riderinformation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userProtalId",id);
        Riderinformation riderinformation = riderinformationMapper.selectOneByExample(example);
        accountOrders.setTodayMoney(riderinformation.getTodayMoney());
        accountOrders.setOrdersNumber(riderinformation.getOrdersNumber());
        return CommonResult.ok(accountOrders);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据骑手用户ID查询骑手余额以及今日收入
     * @Date: 2022/10/27 13:37
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: [id]
     */

    @Override
    public CommonResult getRiderMoney(Integer userProtalId) {
        AccountOrders accountOrders = new AccountOrders();
        Example example = new Example(Riderinformation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userProtalId",userProtalId);
        Riderinformation riderinformation = riderinformationMapper.selectOneByExample(example);
        return CommonResult.ok(riderinformation);
    }
}
